﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//在自身前方2米生成一个物体
public class Test_Transform1 : MonoBehaviour
{
    private void Start()
    {
        GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
        go.transform.position = transform.position + transform.forward * 2;
    }
}

//父物体和根物体
public class Test_Transform2 : MonoBehaviour
{
    private void Start()
    {
        transform.parent.name = "AAA";
        transform.root.name = "BBB";
    }
}

//Transform移动和旋转，简单的变换方式，角色一般不用这个 ，角色用CharacterController
public class Test_Transform3 : MonoBehaviour
{
    public float speed = 5f;//速度
    public float rotateSpeed = 270;//旋转速度

    //获取按键的方式
    private void Update1()
    {
        //transform.Translate(Vector3.forward);//每帧走1米
        if (Input.GetKey(KeyCode.W))
        {
            transform.Translate(Vector3.forward * Time.deltaTime * speed);// 每秒走speed米 Time.deltaTime  这一帧消耗的时间
        }
        if (Input.GetKey(KeyCode.S))
        {
            transform.Translate(Vector3.forward * Time.deltaTime * -speed);
        }
        if (Input.GetKey(KeyCode.A))
        {
            transform.Rotate(Vector3.up * Time.deltaTime * rotateSpeed);
        }
        if (Input.GetKey(KeyCode.D))
        {
            transform.Rotate(Vector3.up * Time.deltaTime * -rotateSpeed);
        }
    }

    //虚拟轴的方式
    private void Update()
    {
        float h = Input.GetAxisRaw("Horizontal");
        float v = Input.GetAxisRaw("Vertical");
        transform.Translate(Vector3.forward * Time.deltaTime * speed * v);
        transform.Rotate(Vector3.up * rotateSpeed * Time.deltaTime * h);
    }
}

//演示围绕旋转
public class Test_Transform4 : MonoBehaviour
{
    public Transform other;

    private void Update()
    {
        transform.RotateAround(other.position, Vector3.up, 90 * Time.deltaTime);//(位置，轴，角度)
    }
}

//演示设置父物体
public class Test_Transform5 : MonoBehaviour
{
    private void Start()
    {
        transform.SetParent(Camera.main.transform);
    }
}

//演示找子物体1通过索引找   2通过名字找
public class Test_Transform6 : MonoBehaviour
{
    private void Start()
    {
        Transform camTf = Camera.main.transform;
        //索引找子物体
        camTf.GetChild(0).name = "ABCD";
        //for遍历子物体
        for (int i = 0; i < camTf.childCount; i++)
        {
            camTf.GetChild(i).gameObject.GetComponent<MeshRenderer>().material.color = Color.cyan;
        }
        //foreach遍历子物体
        foreach (Transform item in camTf)
        {
            print(item.name);
        }
        //根据名字找子物体
        camTf.Find("Sphere/Cylinder").GetComponent<Renderer>().material.color = Color.black;
    }
}

//演示从子物体获取组件
internal class Test_Transform7 : MonoBehaviour
{
    //找一个
    private void Start1()
    {
        GetComponentInChildren<Test_D02>().i = 200;
        //如果挂脚本的物体被禁用可以这样找
        //Camera.main.transform.Find("Sphere/Cylinder").GetComponent<Test_D02>().i = 300;
    }

    //找多个
    private void Start()
    {
        Test_D02[] array = GetComponentsInChildren<Test_D02>();
        for (int i = 0; i < array.Length; i++)
        {
            array[i].i = 500;
        }
    }
}

//把相对于世界原点的向量转换成自身方向的向量
public class Test_Transform8 : MonoBehaviour
{
    public GameObject go;
    public GameObject go1;

    private void Start()
    {
        Vector3 v = go.transform.TransformDirection(Vector3.one);
        go.transform.position = go1.transform.position + v;
    }
}

//把相对于世界原点的点转换成自身坐标系中的点
public class Test_Transform9 : MonoBehaviour
{
}

//设置在父物体中索引位置
public class Test_Transform10 : MonoBehaviour
{
    public int index;

    private void Start()
    {
        transform.SetSiblingIndex(index);
    }
}

//设置为首个子物体    设置为最后一个子物体
public class Test_Transform11 : MonoBehaviour
{
    private void Start1()
    {
        transform.SetAsFirstSibling();
    }

    private void Start()
    {
        transform.SetAsLastSibling();
    }
}

//LookAt()  看着
public class Test_Transform12 : MonoBehaviour
{
    public GameObject a;
    public GameObject b;

    private void Update()
    {
        a.transform.LookAt(b.transform);
    }
}